package inboundandoutbound;/*

 */

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * @ClassName MyClientHandler
 * @Author Final
 * @Project DataStructures
 */
public class MyClientHandler extends SimpleChannelInboundHandler<Long> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Long aLong) throws Exception {

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        System.out.println("MyClientHandler发送数据");
        //ctx.writeAndFlush(123L);
        /**
         * MessageToByteEncoder
         *  判断是不是应该处理的类型
         *      是就去处理，不是就跳过
         *
         *  在这string类型就跳过处理
         *         public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
         *         ByteBuf buf = null;
         *
         *         try {
         *             if (this.acceptOutboundMessage(msg)) {
         *                 I cast = msg;
         *                 buf = this.allocateBuffer(ctx, msg, this.preferDirect);
         *
         *                 try {
         *                     this.encode(ctx, cast, buf);
         *                 } finally {
         *                     ReferenceCountUtil.release(msg);
         *                 }
         *
         *                 if (buf.isReadable()) {
         *                     ctx.write(buf, promise);
         *                 } else {
         *                     buf.release();
         *                     ctx.write(Unpooled.EMPTY_BUFFER, promise);
         *                 }
         *
         *                 buf = null;
         *             } else {
         *                 ctx.write(msg, promise);
         *             }
         *         } catch (EncoderException var17) {
         *             throw var17;
         *         } catch (Throwable var18) {
         *             throw new EncoderException(var18);
         *         } finally {
         *             if (buf != null) {
         *                 buf.release();
         *             }
         *
         *         }
         *
         *     }
         *
         *     传入得数要和处理的类型一致
         */
        ctx.writeAndFlush("aaaxxxxaaaaddddaaaa");


    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

        cause.printStackTrace();
        ctx.close();
    }
}
